package com.liiy.missyou.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.liiy.missyou.exception.http.ServerErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GenericAndJson {

    private static ObjectMapper mapper;

    @Autowired
    public void setMapper(ObjectMapper mapper){
        GenericAndJson.mapper = mapper;
    }

    public static <T> String objectToJson(T o){
        try {
            return GenericAndJson.mapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            throw new ServerErrorException(9999);
        }
    }

    public static <T> T jsonToObject(String s, Class<T> classT){
        if(s == null){
            return null;
        }
        try {
            T o = GenericAndJson.mapper.readValue(s,classT);
            return o;
        } catch (JsonProcessingException e) {
            throw new ServerErrorException(9999);
        }
    }

    public static <T> T jsonToList(String s, TypeReference<T> tr){
        if(s == null){
            return null;
        }
        try {
            T list = GenericAndJson.mapper.readValue(s, tr);
            return list;
        } catch (JsonProcessingException e) {
            throw new ServerErrorException(9999);
        }
    }
}
